<?php

namespace app\advertisement\logic;

use think\facade\Db;
use think\facade\Cache;
use util\Excel;
use think\exception\ValidateException;
use think\facade\Log;
use util\Token;
use util\Upload;
use think\cache\driver\Redis;

class AdvcampaignLogic
{
    public function __construct()
    {
        
    }

    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 列表
     * @author zhangheng
     */
    public function index($params = [])
    {
        $limit = $params['limit'] ?? 10;
        $advertiser_id = $params['advertiser_id'] ?? "";
        $createtime = $params['createtime'] ?? "";
        $campaign_id = $params['campaign_id'] ?? "";
        $platform_id = $params['platform_id'] ?? 0;

        $where = [];
        $where[] = ['type', '=', 0];

        if (!empty($createtime)) {
            $where[] = ['stat_time', '>=', strtotime($createtime['0'])];
            $where[] = ['stat_time', '<=', strtotime($createtime['1']) + 3600*24-1];
        }else{
            $yesterday = strtotime(date('Y-m-d', strtotime('-1 day') - timezone())); 
            $where[] = ['stat_time', '>=', $yesterday];
            $where[] = ['stat_time', '<=', $yesterday + 3600*24-1];
        }

        if (!empty($advertiser_id)) {
            $where[] = ['advertiser_id', 'like', '%' . $advertiser_id . '%'];
        }
        if (!empty($campaign_id)) {
            $where[] = ['campaign_id', 'like', '%' . $campaign_id . '%'];
        }
        if (!empty($status)) {
            $where[] = ['status', '=', $status];
        }
        if (!empty($platform_id)) {
            $where[] = ['platform_id', '=', $platform_id];
        }

        $list = Db::name('advertising_campaign')
            ->field('
                campaign_id,
                advertiser_id,
                stat_time_day,
                conversion,
                onsite_shopping_roas,
                campaign_name,
                total_onsite_shopping_value,
                ad_name,
                real_time_conversion,
                platform_id,
                spend
            ')
            ->where($where)
            ->paginate($limit);
        $lists = $list->items();

        foreach ($lists as $k=>$v) {
            switch ($v['platform_id']) {
                case '1':
                    $lists[$k]['platform'] = 'Facebook';
                    break;
                case '2':
                    $lists[$k]['platform'] = 'Google';
                    break;
                case '3':
                    $lists[$k]['platform'] = 'TikTok';
                    break;
                default:
                    $lists[$k]['platform'] = '未知';
            }
        }

        $data = [
            'list' => $lists,
            'total' => $list->total(),
        ];
        return json(DefaultResponse(200, true, '获取成功', $data));
    }

    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 导出
     * @author zhangheng
     */
    public function export($params = [])
    {
        $advertiser_id = $params['advertiser_id'] ?? "";
        $createtime = $params['createtime'] ?? "";
        $campaign_id = $params['campaign_id'] ?? "";

        $where = [];

        if (!empty($createtime)) {
            $where[] = ['stat_time', '>=', strtotime($createtime['0'])];
            $where[] = ['stat_time', '<=', strtotime($createtime['1']) + 3600*24-1];
        }else{
            $yesterday = strtotime(date('Y-m-d', strtotime('-1 day') - timezone())); 
            $where[] = ['stat_time', '>=', $yesterday];
            $where[] = ['stat_time', '<=', $yesterday + 3600*24-1];
        }

        if (!empty($advertiser_id)) {
            $where[] = ['advertiser_id', 'like', '%' . $advertiser_id . '%'];
        }
        if (!empty($campaign_id)) {
            $where[] = ['campaign_id', 'like', '%' . $campaign_id . '%'];
        }
        if (!empty($status)) {
            $where[] = ['status', '=', $status];
        }

        $lists = Db::name('advertising_campaign')
            ->field('
                campaign_id,
                advertiser_id,
                stat_time_day,
                conversion,
                onsite_shopping_roas,
                campaign_name,
                total_onsite_shopping_value,
                ad_name,
                real_time_conversion,
                spend
            ')
            ->where($where)
            ->select()
            ->toarray();

        $header = [];
        $header[] = ['广告账户', 'advertiser_id'];
        $header[] = ['系列id', 'campaign_id'];
        $header[] = ['时间', 'stat_time_day'];
        $header[] = ['花费', 'spend'];
        $header[] = ['转换率', 'conversion'];
        $header[] = ['ROI', 'onsite_shopping_roas'];
        $header[] = ['系列名称', 'campaign_name'];
        $header[] = ['收入', 'total_onsite_shopping_value'];
        $header[] = ['广告名称', 'ad_name'];
        $header[] = ['实时转换', 'real_time_conversion'];

        $fileName = "export_Advplatform" . time();
        return Excel::exportData($lists, $header, $fileName, 'xlsx');
    }
    
}
